package com.cqupt.controller;

import com.cqupt.pojo.ResponseMessage;
import com.cqupt.service.DemoService;
import com.cqupt.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("demo")
@ResponseBody
public class DemoController {
    @Autowired
    private DemoService demoService;

    /**
     * 获取监控项
     * @param params
     * @return
     */
    @RequestMapping("/get_utilization_by_name")
    public ResponseMessage getUtilizationByName(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        String hostId = params.get("hostId").toString();
        String name = params.get("name").toString();
        final List<Map<String, Object>> resultList = demoService.getUtilizationByName(hostId, name);
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", resultList);
        }};
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 获取主机列表
     * @return
     */
    @RequestMapping("/get_all_online_host")
    public ResponseMessage getAllOnlineHost() {
        ResponseMessage responseMessage = new ResponseMessage();
        final List<Map<String, Object>> resultList = demoService.getAllOnlineHost();
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", resultList);
        }};
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 分页获取所有在线主机
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @return 封装的返回消息，含result(所有结果)和dataNum(数据总数)
     */
    @RequestMapping("/get_pageable_online_host")
    public ResponseMessage getPageableOnlineHost(@RequestParam(value = "currentPage", required = false) String currentPage,
                                                  @RequestParam(value = "pageSize", required = false) String pageSize) {
        ResponseMessage temp = new ResponseMessage();

        final Map<String, Object> resultMap = demoService.getPageableOnlineHost(currentPage, pageSize);
        ResponseMessage responseMessage = TextUtils.packDataToResponseMessage(temp, resultMap);

        return responseMessage;
    }
}
